package com.jic.member.controller.power;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MegradeRule;
import com.jic.member.request.power.MememberLegalRuleRequest;
import com.jic.member.response.power.MegradeRuleResponse;
import com.jic.member.service.power.MegradeRuleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.stream.Collectors;

@ApiOperation(value = "会员等级权益关联controller")
@RestController
@RequestMapping("/megradeRule")
@Slf4j
public class MegradeRuleController {


    @Autowired
    MegradeRuleService megradeRule;

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取等级权益关联所有列表")
    @ResponseBody
    @RequestMapping("/queryMegradeRuleList")
    public RestResult<PageResult<MegradeRuleResponse>> queryMegradeRuleList(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入等级权益关联列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        PageResult<MegradeRule> result = megradeRule.queryPage(memberRequest, page);
        List<MegradeRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MegradeRuleResponse memberResponse = BeanUtil.copy(temp, MegradeRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MegradeRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取等级权益关联列表")
    @ResponseBody
    @RequestMapping("/queryMegradeRuleListForLook")
    public RestResult<PageResult<MegradeRuleResponse>> queryMegradeRuleListForLook(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入等级权益关联列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        PageResult<MegradeRule> result = megradeRule.queryPage(memberRequest, page);
        List<MegradeRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MegradeRuleResponse memberResponse = BeanUtil.copy(temp, MegradeRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MegradeRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "查询列表不分页")
    @ResponseBody
    @RequestMapping("/queryMegradeRuleListOther")
    public RestResult queryMegradeRuleListOther(@RequestBody MememberLegalRuleRequest form) {
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        List<MegradeRule> query = megradeRule.query(memberRequest);
        return RestResult.success(query);
    }

    @ApiOperation(value = "保存方法")
    @ResponseBody
    @RequestMapping("/saveGradeRule")
    public RestResult saveGradeRule(@RequestBody MememberLegalRuleRequest form){
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        megradeRule.insert(memberRequest);
        return RestResult.success("1");
    }

    @ApiOperation(value = "查询列表不分页")
    @ResponseBody
    @RequestMapping("/queryMegradeRuleInfo")
    public RestResult queryMegradeRuleInfo(@RequestBody MememberLegalRuleRequest form) {
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        MegradeRule query = megradeRule.queryByPrimaryKey(memberRequest.getId());
        return RestResult.success(query);
    }

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取等级权益关联单个信息")
    @ResponseBody
    @RequestMapping("/queryMegradeRuleSimple")
    public RestResult<PageResult<MegradeRuleResponse>> queryMegradeRuleSimple(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入等级权益关联列表=========》,{}", form.toString());
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        PageResult<MegradeRule> result = megradeRule.queryMegradeRuleSimple(memberRequest);
        List<MegradeRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MegradeRuleResponse memberResponse = BeanUtil.copy(temp, MegradeRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MegradeRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }
}
